from app import create_app, socketio
import os
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('app.log', encoding='utf-8'),
        logging.StreamHandler()
    ]
)

app = create_app()

if __name__ == '__main__':
    # 获取配置
    host = os.environ.get('HOST', '0.0.0.0')
    port = int(os.environ.get('PORT', 7722))
    debug = os.environ.get('DEBUG', 'False').lower() == 'true'
    
    print(f"🚀 启动机械臂控制系统...")
    print(f"📍 服务地址: http://{host}:{port}")
    print(f"🔧 调试模式: {debug}")
    print(f"📊 WebSocket支持: 已启用")
    
    try:
        # 使用SocketIO运行应用
        socketio.run(
            app, 
            host=host, 
            port=port, 
            debug=debug, 
            use_reloader=False,
            allow_unsafe_werkzeug=True,
            log_output=True
        )
    except KeyboardInterrupt:
        print("\n👋 应用已停止")
    except Exception as e:
        print(f"❌ 启动失败: {e}")
        logging.error(f"应用启动失败: {e}")